package com.lanlian.cayman.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import android.widget.RadioGroup;

import com.lanlian.cayman.R;
import com.lanlian.cayman.fragment.RiskResearchFourFragment;
import com.lanlian.cayman.fragment.RiskResearchOneFragment;
import com.lanlian.cayman.fragment.RiskResearchThreeFragment;
import com.lanlian.cayman.fragment.RiskResearchTwoFragment;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqRiskState;
import com.lanlian.commonlib.bean.db.DBUser;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.uikit.ViewPagerSlide;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * <pre>
 *     date   : 2018/8/14 14:25
 *     desc   : 投资风险调研
 *     version: 1.0
 * </pre>
 */
public class RiskStateActivity extends BaseActivity {

    private RadioGroup mRgRiskResearch;
    private ViewPagerSlide mViewPager;

    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private RiskResearchOneFragment Fragment1;
    private RiskResearchTwoFragment Fragment2;
    private RiskResearchThreeFragment Fragment3;
    private RiskResearchFourFragment Fragment4;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_risk_research, true);
        initView();
        setListeners();
        initFragments();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("投资风险调研");
        mRgRiskResearch = findViewById(R.id.rg_risk_research);
        mViewPager = findViewById(R.id.view_pager);
        mViewPager.setSlide(false);
    }

    private void initFragments() {
        Fragment1 = new RiskResearchOneFragment(new RiskResearchOneFragment.OnNextClickListener() {
            @Override
            public void onNextClick() {
                mViewPager.setCurrentItem(1);
                mRgRiskResearch.check(R.id.rb_experience);
            }
        });
        Fragment2 = new RiskResearchTwoFragment(new RiskResearchTwoFragment.OnNextClickListener() {
            @Override
            public void onNextClick() {
                mViewPager.setCurrentItem(2);
                mRgRiskResearch.check(R.id.rb_situation);
            }

            @Override
            public void onPreviousClick() {
                mViewPager.setCurrentItem(0);
                mRgRiskResearch.check(R.id.rb_purpose);
            }
        });
        Fragment3 = new RiskResearchThreeFragment(new RiskResearchThreeFragment.OnNextClickListener() {
            @Override
            public void onNextClick() {
                mViewPager.setCurrentItem(3);
                mRgRiskResearch.check(R.id.rb_tolerance);
            }

            @Override
            public void onPreviousClick() {
                mViewPager.setCurrentItem(1);
                mRgRiskResearch.check(R.id.rb_experience);
            }
        });
        Fragment4 = new RiskResearchFourFragment(new RiskResearchFourFragment.OnNextClickListener() {
            @Override
            public void onNextClick() {

            }

            @Override
            public void onPreviousClick() {
                mViewPager.setCurrentItem(2);
                mRgRiskResearch.check(R.id.rb_situation);
            }
        });
        mFragments.add(Fragment1);
        mFragments.add(Fragment2);
        mFragments.add(Fragment3);
        mFragments.add(Fragment4);
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return mFragments.get(i);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        });
        mRgRiskResearch.check(R.id.rb_purpose);
        mViewPager.setOffscreenPageLimit(mFragments.size());
    }

    public String getData() {
        String s1 = null;
        try {
            String phone = getIntent().getStringExtra("mobile");
            JSONObject data = new JSONObject();
            data.put("mobile", phone);
            JSONArray ja = new JSONArray();
            ArrayList<JSONObject> list = new ArrayList<>();
            ArrayList<JSONObject> list1 = Fragment1.getData();
            ArrayList<JSONObject> list2 = Fragment2.getData();
            ArrayList<JSONObject> list3 = Fragment3.getData();
            ArrayList<JSONObject> list4 = Fragment4.getData();
            list.addAll(list1);
            list.addAll(list2);
            list.addAll(list3);
            list.addAll(list4);
            for (int i = 0; i < list.size(); i++) {
                ja.put(list.get(i));
            }
            data.put("list", ja);
            s1 = data.toString();
            Log.e("test", s1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s1;
    }

    public void tijiao() {
        ReqRiskState rm = new ReqRiskState();
        rm.data = getData();
        showLoadingDialog();
        VolleyRequestUtils.getInstance().sendRequest(this.toString(), rm, new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                dismissLoadingDialog();
                DBUser user = mDBManager.getUser();
                if (user != null) {
                    user.setRiskAuthState(1);
                    mDBManager.updateUser(user);
                }
                finish();
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                dismissLoadingDialog();
                showToast(baseResponse.getMsg());
            }
        });
    }

}
